/**
 * COPYRIGHT 2021 ALL RESERVED. (C) liaoyulei, https://github.com/dualface
 */

// import { Rect, view } from "cc";
import { ECSSystem } from "../../../3rd/ECS/ECSSystem";
import { ecsclass } from "../../../3rd/ECS/__private";
import { BindNode } from "../../../Bridge/BindNode";
import { GoWarp } from "../../../Bridge/GoWarp";
import { Rect } from "../../../Bridge/Rect";
import NodeHelper from "../../../Bridge/NodeHelper";
import Util from "../../../Framework/Util/Util";
import { GameRT } from "../../GameRT";
import { MovableComponent } from "../components/MovableComponent";
import { RenderNodeComponent } from "../components/RenderNodeComponent";
import { BackStartPoint, UserInputEvent } from "../events/GameEvent";
import { PositionComponent } from "../components/PositionComponent";
import { UserCommand } from "../components/InputComponent";
import { MathBirde } from "../../../Bridge/Math";
import { GameComponent } from "../components/GameComponent";


/**
 * 处理所有可以移动的组件
 */
@ecsclass("MovableSystem")
export class MovableSystem extends ECSSystem {
 
    readonly jumpHeight = 2.0;

    /**
     * 跳起来用时多少帧
     */
    readonly jumpFrame = 60; 
    /**
     * 构造函数
     */
    constructor() {
        super();
    }

    /**
     * 更新可移动组件的位置
     *
     * @param dt
     */
    update(dt: number): void {
        // 从 ECS 取得所有 MovableComponent
        const movables = this.ecs.components.all(MovableComponent);

        // Util.Log("moveable update",movables);
       

        // 遍历这些 MovableComponent，并更新状态
        movables.forEach((movable) => {
            const entity = this.ecs.entities.get(movable.entityId);
            var posComponent = entity.getComponent(PositionComponent);


            
            if (movable.resistance > 0)
            {
                movable.resistance =  movable.resistance -= dt*0.1; //阻力随时间推移消失
                movable.resistance = Math.max(movable.resistance,0); //阻力不能小于0
            }

            posComponent.pos.z += Math.max(movable.speed - movable.resistance,0);
            if (posComponent.pos.z > GameRT.ScenLength)
            {
                this.ecs.events.push(new BackStartPoint());
                posComponent.pos.z=0;
            }

            posComponent.pos.x = MathBirde.LerpNumber(posComponent.pos.x,movable.tweenXpos,0.1);

            const gameComp = this.ecs.components.get(GameComponent); 
            if(gameComp.gameFrame - movable.startJumpFrame < this.jumpFrame) //如果还在跳起来的帧内,就继续向上
            {
                posComponent.pos.y = MathBirde.LerpNumber(posComponent.pos.y,movable.tweenYpos,0.1);
            }
            else
            {
                posComponent.pos.y = MathBirde.LerpNumber(posComponent.pos.y,0,0.1);
            }
            
            
            var userInputEvents = this.ecs.events.fetch(UserInputEvent)
            userInputEvents.forEach(element => {     

                if (element.command == UserCommand.left)
                {
                    movable.tweenXpos-= GameRT.RoadSpace;
                }
                else if (element.command == UserCommand.right)
                {
                    movable.tweenXpos+= GameRT.RoadSpace;
                }
                else if (element.command == UserCommand.up)
                {
                    movable.tweenYpos = this.jumpHeight;
                    movable.startJumpFrame = gameComp.gameFrame;
                }
                movable.tweenXpos = MathBirde.Clamp(movable.tweenXpos,-GameRT.RoadSpace, GameRT.RoadSpace)

            });

           

        });
    }
}
